package com.example.administrator.newproject;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.List;
import Bean.User;
import Utils.DbUtil;


public class ZhuCeDengLuActivity extends ActionBarActivity {




    private EditText user;
    private EditText pass;
    private DbUtils dbUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhu_ce_deng_lu);

        user = (EditText) findViewById(R.id.user);
        pass = (EditText) findViewById(R.id.pass);

        Button  zhuce= (Button) findViewById(R.id.zhuce);
        Button  denglu= (Button) findViewById(R.id.denglu);
        dbUtils = DbUtil.getDbUtils(ZhuCeDengLuActivity.this);
        zhuce.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String userName1 = user.getText().toString();
               String passWord1 = pass.getText().toString();
                if(userName1==null||passWord1==null){
                    Toast.makeText(ZhuCeDengLuActivity.this,"输入内容不完整",Toast.LENGTH_LONG).show();

                }else{
                        try {
                            List<Object> all = dbUtils.findAll(Selector.from(User.class)
                                    .where("name", "=", userName1)
                                    .and(WhereBuilder.b("pass", "=", passWord1))
                                    .orderBy("name").limit(1).offset(0));
                           if(!all.isEmpty()){
                               Toast.makeText(ZhuCeDengLuActivity.this,"已注册",Toast.LENGTH_SHORT).show();

                           }else{

                               User user1 = new User(userName1, passWord1);
                               try {
                                   dbUtils.save(user1);
                                   Toast.makeText(ZhuCeDengLuActivity.this,"注册成功！",Toast.LENGTH_LONG).show();

                                   Intent i1= new Intent();
                                   i1.putExtra("user",userName1);
                                   i1.setClass(ZhuCeDengLuActivity.this,SecondActivity.class);
                                   startActivity(i1);

                               } catch (DbException e) {
                                   e.printStackTrace();
                               }
                           }

                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                }
          }
        });

        denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName2 = user.getText().toString();
                String passWord2 = pass.getText().toString();

                try {
                    List<Object> all = dbUtils.findAll(Selector.from(User.class)
                            .where("name", "=", userName2)
                            .and(WhereBuilder.b("pass", "=", passWord2))
                            .orderBy("name").limit(1).offset(0));
                    if(all.isEmpty()){
                        Toast.makeText(ZhuCeDengLuActivity.this,"请先注册",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(ZhuCeDengLuActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                        Intent i2= new Intent();
                        i2.putExtra("user",userName2);
                        i2.setClass(ZhuCeDengLuActivity.this,SecondActivity.class);
                        startActivity(i2);
                    }

                } catch (DbException e) {
                    e.printStackTrace();
                }

            }
        });

    }
}
